package me.zhengjie.modules.transformer.history.service.impl;

import me.zhengjie.modules.transformer.history.domain.FdsData;
import me.zhengjie.modules.transformer.history.domain.FdsRecord;
import me.zhengjie.modules.transformer.history.repository.FdsDataRepository;
import me.zhengjie.modules.transformer.history.repository.FdsRecordRepository;
import me.zhengjie.modules.transformer.history.service.FdsDataService;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.transformer.history.service.FdsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈佳豪
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2021-12-02
 **/
@Service
@RequiredArgsConstructor
public class FdsDataServiceImpl implements FdsDataService {
    @Autowired
    FdsDataRepository fdsDataRepository;
    @Autowired
    FdsRecordService fdsRecordService;
    @Autowired
    FdsRecordRepository fdsRecordRepository;

    @Override
    public void importFile(File file) {
        readCSV(file);
    }

    @Override
    public List<FdsData> findByRecordId(int recordId) {
        return fdsDataRepository.findByRecordId(recordId);
    }


    public void readCSV(File file) {
        int newRecordId = fdsRecordService.getNewRecordId();
        List<FdsData> fdsDataList = new ArrayList<>();
        file.setReadable(true);//设置可读
//            csv.setWritable(true);//设置可写
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        Boolean startScan = false;
        String[] data;
        String regex = "ID,Frequency,Tan Delta,Power Factor,e',e\"\",c',c\"\".*";
        String regexEnd = ",*";
        try {
            while ((line = br.readLine()) != null) // 读取到的内容给line变量
            {
                if (line.matches(regex)) {
                    startScan = true;
                    continue;
                }
                if ((line.matches(regexEnd) || line.length() == 0) && startScan) {
                    break;
                }
                if (startScan == true) {
                    data = line.split(",");
//                    String equip_id = "批次";
//                    String record_id = "批次";
                    String frequency = data[1];
                    String tan_delta = data[2];
                    String power_factor = data[3];
                    String e_real = data[4];
                    String e_imaginary = data[5];
                    String c_real = data[6];
                    String c_imaginary = data[7];
                    FdsData fdsData = new FdsData();
                    fdsData.setFrequency(frequency);
                    fdsData.setTanDelta(tan_delta);
                    fdsData.setPowerFactor(power_factor);
                    fdsData.setEReal(e_real);
                    fdsData.setEImaginary(e_imaginary);
                    fdsData.setCReal(c_real);
                    fdsData.setCImaginary(c_imaginary);
                    fdsData.setEquipId(newRecordId);
                    fdsData.setRecordId(newRecordId);
                    fdsDataList.add(fdsData);
//                    System.out.println(frequency + ' ' + tan_delta + " " + power_factor + " " + e_real + " " + e_imaginary + " " + c_real + " " + c_imaginary);
//                    System.out.println("__________");
                }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        fdsDataRepository.saveAll(fdsDataList);
        FdsRecord fdsRecord = new FdsRecord();
        fdsRecord.setRecordId(newRecordId);
        fdsRecordRepository.save(fdsRecord);
    }

}
